<?php

/**
 * escalasnormales actions.
 *
 * @package    SistemaRutasEc
 * @subpackage escalasnormales
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class escalasnormalesActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {


        $idru = $request->getParameter('id_ru');
        $this->getUser()->setAttribute('id_ru', $idru);
        $q = Doctrine_Query::create()
                ->from('ruta r')
                ->where('r.id_ru=?', $idru);
        $this->ruta = $q->fetchOne();

        $q2 = Doctrine_Query::create()
                ->from('cooperativa c')
                ->where('c.id_co=?', $this->ruta->getIdCo());
        $this->coop = $q2->fetchOne();

        $this->escalanormals = Doctrine::getTable('escalanormal')->createQuery('en')
                ->where('en.id_ru = ?', $idru)
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->escalanormal = Doctrine_Core::getTable('escalanormal')->find(array($request->getParameter('id_en')));
        $this->forward404Unless($this->escalanormal);
    }

    public function executeNew(sfWebRequest $request) {
        $this->idru = $request->getParameter('id_ru');
        $this->form = new escalanormalForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->idru = $this->getUser()->getAttribute('id_ru');
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new escalanormalForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->idru = $request->getParameter('id_ru');
        $this->forward404Unless($escalanormal = Doctrine_Core::getTable('escalanormal')->find(array($request->getParameter('id_en'))), sprintf('Object escalanormal does not exist (%s).', $request->getParameter('id_en')));
        $this->form = new escalanormalForm($escalanormal);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($escalanormal = Doctrine_Core::getTable('escalanormal')->find(array($request->getParameter('id_en'))), sprintf('Object escalanormal does not exist (%s).', $request->getParameter('id_en')));
        $this->form = new escalanormalForm($escalanormal);

        $this->processFormEdit($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($escalanormal = Doctrine_Core::getTable('escalanormal')->find(array($request->getParameter('id_en'))), sprintf('Object escalanormal does not exist (%s).', $request->getParameter('id_en')));
        $aux = $escalanormal->getIdRu();
        $escalanormal->delete();
        $this->getUser()->setFlash('notice', 'Frecuencia eliminada exitosamente!');
        $this->redirect('escalasnormales/index?id_ru=' . $aux);
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {

            $form->getObject()->setIdRu($this->getUser()->getAttribute('id_ru'));
            $escalanormal = $form->save();
            $this->getUser()->setFlash('notice', 'Frecuencia agregada exitosamente!');
            $this->redirect('escalasnormales/index?id_ru=' . $escalanormal->getIdRu());
        }
    }
    
    protected function processFormEdit(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {

            
            $escalanormal = $form->save();
            $this->getUser()->setFlash('notice', 'Frecuencia editada exitosamente!');
            $this->redirect('escalasnormales/index?id_ru=' . $escalanormal->getIdRu());
        }
    }
    

}
